<?php
namespace App\Models\Prize;

use App\Contracts\Repositories\PrizeRepository as PrizeRepository;
use Illuminate\Support\Facades\Log;

class PrizeService
{
    private $prizeResposity;

    public function __construct(PrizeRepository $prizeRepository)
    {
        $this->prizeResposity = $prizeRepository;
    }

    public function create($prize)
    {
        return $this->prizeResposity->create($prize);
    }

    public function modify($prize, $conditions)
    {
        return $this->prizeResposity->modify($prize, $conditions);
    }

    public function delete($conditions, $softDeletion = true)
    {
        return $this->prizeResposity->delete($conditions, $softDeletion);
    }

    public function getPrizeById($id)
    {
        return $this->prizeResposity->findById($id);
    }

    public function getPrizeByType($type)
    {
        return $this->prizeResposity->findByType($type);
    }

    public function getPrizeByConditions($conditions, $withs, $limit = 10, $offset = null)
    {
        return $this->prizeResposity->findByConditions($conditions, $withs, $limit, $offset);
    }

    public function getPrizeDetail($conditions, $withs)
    {
        return $this->prizeResposity->findDetailByConditions($conditions, $withs);
    }

    public function getPaginateByConditions($conditions, $withs, $limit, $offset)
    {
        $prizeInfos = $this->prizeResposity->findPaginateByConditions($conditions, $withs, $limit, $offset);
        foreach ($prizeInfos['data'] as $index => $datum) {
            $total = 0;
            foreach ($datum['prize_record'] as $prizeRecord) {
                if ($prizeRecord['operate'] != 'delivery' && empty($prizeRecord['activity_id'])) {
                    $total += $prizeRecord['number'];
                }
                if ($prizeRecord['operate'] == 'delivery' && empty($prizeRecord['activity_id'])) {
                    $total -= $prizeRecord['number'];
                }
            }
            $prizeRecordSummary = array_column($datum['prize_record'], 'summary', 'updated_at');
            ksort($prizeRecordSummary);
            $prizeInfos['data'][$index]['stock_number'] = $total;
            $availableStock = end($prizeRecordSummary);
            $prizeInfos['data'][$index]['available_stock'] = $availableStock;
        }
        return $prizeInfos;
    }

    /**
     * 统计奖品使用情况
     * @param array $conditions
     * @return array
     */
    public function settleActivity($conditions)
    {
        $prizeInfos = $this->prizeResposity->findByConditions($conditions, array('prizeType', 'sponsor', 'activity'));
        foreach ($prizeInfos as $prizeInfo) {
            $sum = 0;
            foreach ($prizeInfo['activity'] as $activity) {
                $sum = $sum + $activity['summary'];
            }
            $settleInfo[] = array(
                'id' => $prizeInfo['id'],
                'name' => $prizeInfo['name'],
                'total' => $prizeInfo['stock_number'],
                'used' => $sum,
                'remain' => $prizeInfo['stock_number'] - $sum
            );
        }
        return $settleInfo;
    }
}
